<?php namespace Admin\Controller;
//品牌控制器
class BrandController extends CommonController
{
    private $model;

    public function __auto()
    {
        $this->model=new \Common\Model\Brand;
    }

    //显示品牌列表首页
    public function index()
    {
        //如果列表没有品牌，跳到添加页面
        $data=$this->model->orderby('bsort','DESC')->get();
        if (!$data) {
            View::success('请先去添加类型', U('add'));
        }
        View::with('data', $data);
        View::make();
    }
    //品牌添加
    public function add(){
        if(IS_POST){
            if($this->model->store()){
                View::success("添加成功",U('index'));
            }
            View::error($this->model->getError());
        }
        View::make();
    }
    //品牌编辑
    public function edit(){
        if(IS_POST){
            if($this->model->edit()){
                View::success('编辑成功',U('index'));
            }
            View::error($this->model->getError());
        }
        $bid=Q('get.bid',0,'intval');
        $data=$this->model->where("bid={$bid}")->find();
        P($data);
        View::with('data',$data);
        View::make();
    }
    public function del(){
        $bid=Q('get.bid',0,'intval');
       
        $old=$this->model->where("bid={$bid}")->find(); 
        $this->model->where("bid={$bid}")->delete();
        // P($old);die;
        $img=$old['logo'];
        if($img){
                unlink($img);
                $path=str_replace("_thumb",'',$img);
                unlink($path);
            }
        View::success("删除成功",U('index'));
    }
}